package www.reagnetbox.com.util;

import android.content.Context;

import com.lxj.xpopup.XPopup;
import com.lxj.xpopup.core.BasePopupView;
import com.lxj.xpopup.interfaces.OnConfirmListener;
import com.lxj.xpopup.interfaces.OnSelectListener;
import com.lxj.xpopup.util.XPopupUtils;

import java.util.List;

import www.reagnetbox.com.callback.SimpleCallback;
import www.reagnetbox.com.callback.StringCallback;

public class XPopupUtil {
    private static BasePopupView loadingPopup;

    public static void showKnowPopup(Context context, String title, String content) {
        new XPopup.Builder(context).maxWidth((int) (XPopupUtils.getAppWidth(context) * .30f)).asConfirm(title, content,
                "", "知道了", null, null, true).show();
    }

    public static void showKnowPopup(Context context, String title, float width, String content) {
        new XPopup.Builder(context).maxWidth((int) (XPopupUtils.getAppWidth(context) * width)).isCenterHorizontal(false).asConfirm(title, content,
                "", "知道了", null, null, true).show();
    }

    public static void showKnowPopup(Context context, String content) {
        new XPopup.Builder(context)
                .maxWidth((int) (XPopupUtils.getAppWidth(context) * .30f))
                .asConfirm(
                        "提示",
                        content,
                        "",
                        "知道了",
                        null,
                        null,
                        true).show();
    }

    public static void showKnowPopup(Context context, float width, String title, String content) {
        new XPopup.Builder(context)
                .maxWidth((int) (XPopupUtils.getAppWidth(context) * width))
                .asConfirm(
                        title,
                        content,
                        "",
                        "知道了",
                        null,
                        null,
                        true).show();
    }

    public static void confirm(Context context, String content, OnConfirmListener onConfirmListener) {
        new XPopup.Builder(context)
                .maxWidth((int) (XPopupUtils.getAppWidth(context) * .30f))
                .asConfirm(
                        "提示",
                        content,
                        "取消",
                        "确定",
                        onConfirmListener,
                        null,
                        false).show();
    }


    public static void input(Context context, float width, String title, String input, StringCallback callback) {
        new XPopup.Builder(context)
                .maxWidth((int) (XPopupUtils.getAppWidth(context) * width))
                .asInputConfirm(title, "", input, "", callback::fun).show();
    }

    public static void singleSel(Context context, float maxWidth, String title, List<?> list, String selected, OnSelectListener onSelectListener) {
        String[] options = new String[list.size()];
        int checkedPosition = -1;
        if (!ValidateUtil.isListValid(list)) {
            return;
        }
        for (int i = 0; i < list.size(); i++) {
            options[i] = list.get(i).toString();
            if (ValidateUtil.isStringValid(selected)) {
                if (selected.equals(list.get(i))) {
                    checkedPosition = i;
                }
            }
        }
        new XPopup.Builder(context)
                .maxWidth((int) (XPopupUtils.getAppWidth(context) * maxWidth))
                .asBottomList(title == null ? "请选择" : title,
                        options,
                        null,
                        checkedPosition,
                        onSelectListener).show();
    }

    public static void showInputPopup(Context context, String title, String content, String hint, String inputText, StringCallback callback) {
        new XPopup.Builder(context).autoFocusEditText(false).asInputConfirm(title, content, inputText, hint,
                callback::fun).show();
    }

    public static void showConfirmPopup(Context context, String title, String content, SimpleCallback callback) {
        new XPopup.Builder(context)
                .maxWidth((int) (XPopupUtils.getAppWidth(context) * .40f)).asConfirm(
                title == null ? "提示" : title,
                content,
                "取消", "确定",
                callback::fun, null, false).show();
    }

    public static void showCustomPopup(Context context, BasePopupView customPopupInstance) {
        new XPopup.Builder(context)
                .autoFocusEditText(false)
                .moveUpToKeyboard(true)
                .dismissOnTouchOutside(false)
                .asCustom(customPopupInstance).show();
    }


    public static void startLoading(Context context, String msg) {
        loadingPopup = new XPopup.Builder(context)
                .asLoading(msg)
                .show();
    }

    public static void stopLoading() {
        if (loadingPopup != null) {
            loadingPopup.dismiss();
        }
    }
}
